home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 10157 < prev    next >
Encoding:
Text File  |  1996-08-05  |  4.8 KB  |  164 lines

  1. Path: rznews.rrze.uni-erlangen.de!news
  2. From: elmar.vogt@rzmail.uni-erlangen.de (Elmar Vogt)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Question about header files
  5. Date: 6 Mar 1996 13:06:38 GMT
  6. Organization: LUR, University of Erlangen.
  7. Message-ID: <4hk2ku$irl@rznews.rrze.uni-erlangen.de>
  8. References: <4hg50t$924@vodka.intele.net>
  9. NNTP-Posting-Host: faust207.lstm.uni-erlangen.de
  10. Mime-Version: 1.0
  11. Content-Type: Text/Plain; charset=US-ASCII
  12. X-Newsreader: WinVN 0.99.7
  13.  
  14. In article <4hg50t$924@vodka.intele.net>, chalain@mcr.net says...
  15. >
  16. >Anyone feeling generous?  I have a question that wants an 
  17. essay-style
  18. >answer....
  19. >
  20.  
  21. I'll give it a shot.
  22.  
  23. >
  24. >In the main file (main.cpp), put the line
  25. >
  26. >#include "myheader.h";
  27. >
  28. >and in myheader.cpp, put the line
  29. >
  30. >#include "myheader.h";
  31. >
  32. >From this, I assume that the compiler, upon opening a *.h file, will
  33. >check for a *.c or *.cpp file of the same name, yes?  
  34.  
  35. Nope. It's just that at compile time, the line
  36. #include "myheader.h"
  37. will be replaced by the contents of said file, line for line.
  38. There's not much more to it.
  39.  
  40. >
  41. >This confuses me just a little bit.  However, it gets worse.
  42. >>
  43. >I assign a pointer to the mode 0x13 video memory with the line
  44. >
  45. >unsigned char far *video_buffer = (unsigned char far *)0xA0000000L;
  46. >
  47. > [in one header file].
  48. I would *really* like to put this line either in the header file or 
  49. in
  50. >the code file for graphics, but if I do I can't get access to the 
  51. pointer
  52. >in MAINFILE or any of its other dependents.
  53.  
  54. I'm not sure, but I'm afraid you've defined the same variable twice 
  55. (assigning it two startup values, which confuses the compiler, 
  56. though they are the same).
  57. Option a):
  58. Write
  59.  
  60. unsigned char far *video_buffer = (unsigned char far *)0xA0000000L;
  61.  
  62. in the .CPP file of your graphics routine, and
  63.  
  64. extern unsigned char far *video_buffer;
  65.  
  66. in all the other source files, or
  67.  
  68. Option b):
  69. Write
  70.  
  71. unsigned char far *video_buffer;
  72.  
  73. in your graphics .H file, and assign it a value in a proper 
  74. intialization routine that's invoked before video_buffer is used:
  75.  
  76. video_buffer = (unsigned char far *)0xA0000000L;
  77.  
  78.  
  79. Alternatively, you could #define it, since it's constant anyway:
  80.  
  81. #define VIDEO_BUFFER    unsigned...
  82.  
  83. (Does this work with a pointer? It's not an LValue this way, is it?)
  84.  
  85. (I'd recommend using the _const_ clause, BTW!)
  86.  
  87. >Be aware that I am using Symantec C++ and am compiling from the DOS 
  88. >prompt, 
  89. >so there aren't any IDE options or project resources that I can add 
  90. or
  91. >tinker with.
  92.  
  93. No, but you should have a lot of compiler options or switches at 
  94. hand. Look at the reference manual for the options.
  95.  
  96. >
  97. >Secondly, I have an extension of my set of graphics function to cope 
  98. with
  99. >PCX graphic files.  These functions are in PCXFILE.H and 
  100. PCXFILE.CPP.
  101. >I would like to include these files as is, but right now the code 
  102. just
  103. >will not compile.  I either get "identifier already defined" at 
  104. compile
  105. >time if I put #include MYGRAPH and #include PCXFILE in MAINFILE or 
  106. >"unknown
  107. >identifier" at link time if I put #include PCXFILE inside MYGRAPH.H. 
  108.  
  109. If MYGRAPH includes PCXFILE, obviously what happens is:
  110.  
  111. MAINFILE -> MYGRAPH -> PCXFILE
  112.     +-----> PCXFILE,
  113.  
  114. ie the same header file is included twice. (Nested includes work 
  115. straightforward.) Usually, compilers can cope with that.
  116. To prevent this though, you can engage conditional compilation.
  117. Put at the head of PCXFILE.H:
  118.  
  119. #ifndef _PCXFILE_ALREADY_INCLUDED_
  120. #define _PCXFILE_ALREADY_INCLUDED_
  121. ... // blah
  122.  
  123. #endif
  124. at the end.
  125. This makes sure that the compilation of the body of PCXFILE.H is 
  126. skipped, if this file already has been included.
  127.  
  128. The second error (identifier unknown) seemingly has to do with the 
  129. link stage.
  130. The compiler works thus:
  131.  
  132. (SOURCE.C + SOURCE.H + INCLUDE.H) -(compile)-> SOURCE.O,
  133.  
  134. where SOURCE.O is an object-code file, roughly machine code.
  135. The linker makes
  136.  
  137. (SOURCE1.O + SOURCE2.O + ...) -(link)-> MYPROG.EXE
  138.  
  139. an exe-file from all the object files.
  140. But at link time, every single object file must known about all the 
  141. variables and functions of all the other object files it invokes.
  142. Thus, if you've omitted to include SOURCE2.H in the compile stage, 
  143. the functions of SOURCE2.CPP will not be known at link stage.
  144.  
  145. >So here are my questions:
  146. >
  147.  
  148. Did I answer a few of them? I'm not sure myself, but if I'm wrong, 
  149. I'll benefit from the corrections from wiser people, too.
  150.  
  151. Elmar
  152.  
  153. -- 
  154. --
  155. /----------------------------------------------------------------\
  156. | Elmar Vogt : MS->PhD | voice: (+49)9131/761-207; fax: ...-202  |
  157. | LUR, Schottkystr. 10 |http://www.rrze.uni-erlangen.de/~iwur08/ |
  158. |University of Erlangen|    Elmar.Vogt@rzmail.uni-erlangen.de    |
  159. |91058 Erlangen-Germany|    Vogt@nt.e-technik.uni-erlangen.de    |
  160. |-------- VIS VISCERIS, NON FERRO FERTUR (T. Doom) --------------|
  161. |SCA: Agilmar von Sevelingen; Fencing, Calligraphy, Jewelry, Food|
  162. \----------------------------------------------------------------/
  163.  
  164.